home *** CD-ROM | disk | FTP | other *** search
/ Symantec Visual Cafe for Java 2.5 / symantec-visual-cafe-2.5-database-dev-edition.iso / Visual Cafe Pro v1.0 / TUTORIAL.BIN / ButtonCell.class (.txt) < prev    next >
Encoding:
Java Class File  |  1997-01-30  |  7.8 KB  |  278 lines

  1. package symantec.itools.db.awt;
  2.  
  3. import java.awt.Color;
  4. import java.awt.Event;
  5. import java.awt.FontMetrics;
  6. import java.awt.Graphics;
  7. import java.awt.Image;
  8. import java.awt.Rectangle;
  9. import java.awt.image.ImageObserver;
  10.  
  11. public class ButtonCell implements TableCell, ImageObserver {
  12.    Grid view;
  13.    DataSource dataSource;
  14.    Coordinate coords;
  15.    boolean pressed;
  16.    boolean drawUp = true;
  17.    boolean defaultFlag;
  18.    int type;
  19.    static final int PADSIDES = 5;
  20.  
  21.    ButtonCell() {
  22.    }
  23.  
  24.    public ButtonCell(Grid v, DataSource ds) {
  25.       this.view = v;
  26.       this.dataSource = ds;
  27.    }
  28.  
  29.    public ButtonCell(int r, int c) {
  30.       this.coords = new Coordinate(r - 1, c - 1);
  31.    }
  32.  
  33.    public int type() {
  34.       return this.type;
  35.    }
  36.  
  37.    public int type(int t) {
  38.       if (t <= 3 && t >= 0) {
  39.          return this.type = t;
  40.       } else {
  41.          throw new IllegalArgumentException("Invalid cell type");
  42.       }
  43.    }
  44.  
  45.    public TableCell cloneCell() {
  46.       ButtonCell bc = new ButtonCell(this.view, this.dataSource);
  47.       if (this.coords != null) {
  48.          bc.coords = new Coordinate(this.coords);
  49.       }
  50.  
  51.       bc.type = this.type;
  52.       bc.defaultFlag = this.defaultFlag;
  53.       return bc;
  54.    }
  55.  
  56.    public void setGrid(Grid v, DataSource ds) {
  57.       this.view = v;
  58.       this.dataSource = ds;
  59.    }
  60.  
  61.    public void setDefaultFlag() {
  62.       this.defaultFlag = true;
  63.    }
  64.  
  65.    public void reset() {
  66.       this.pressed = false;
  67.       this.drawUp = true;
  68.    }
  69.  
  70.    public void setCoordinates(Coordinate p) {
  71.       this.coords = p;
  72.    }
  73.  
  74.    public Coordinate getCoordinates() {
  75.       return this.coords;
  76.    }
  77.  
  78.    public int row() {
  79.       return this.coords.row;
  80.    }
  81.  
  82.    public void setRow(int r) {
  83.       this.coords.row = r;
  84.    }
  85.  
  86.    public int col() {
  87.       return this.coords.col;
  88.    }
  89.  
  90.    public void setCol(int c) {
  91.       this.coords.col = c;
  92.    }
  93.  
  94.    boolean inside(int x, int y) {
  95.       Rectangle r = this.view.getCellBounds(this);
  96.       return x > 0 && y > 0 && x < r.width && y < r.height;
  97.    }
  98.  
  99.    public boolean mouseEvent(Event e) {
  100.       if (e.id == 501) {
  101.          this.view.setCapture(this);
  102.          this.pressed = true;
  103.          this.drawUp = false;
  104.          this.view.generateEvent(e, 50, this);
  105.          this.view.redrawCell(this);
  106.       } else if (e.id == 506) {
  107.          if (this.inside(e.x, e.y) && this.drawUp) {
  108.             this.drawUp = false;
  109.             this.view.generateEvent(e, 52, this);
  110.             this.view.redrawCell(this);
  111.          } else if (!this.inside(e.x, e.y) && !this.drawUp) {
  112.             this.drawUp = true;
  113.             this.view.generateEvent(e, 53, this);
  114.             this.view.redrawCell(this);
  115.          }
  116.       } else if (e.id == 502) {
  117.          if (this.pressed && !this.drawUp) {
  118.             this.view.generateEvent(e, 51, this);
  119.          }
  120.  
  121.          this.pressed = false;
  122.          this.drawUp = true;
  123.          this.view.redrawCell(this);
  124.          this.view.lostCapture();
  125.       }
  126.  
  127.       return true;
  128.    }
  129.  
  130.    public Data getData() throws DataNotAvailable {
  131.       return this.dataSource.getData(this.coords);
  132.    }
  133.  
  134.    String chopString(String text, FontMetrics fm, Rectangle r) {
  135.       int w = r.width;
  136.       int i = 1;
  137.       if (text.length() == 0) {
  138.          return text;
  139.       } else {
  140.          String t;
  141.          do {
  142.             t = text.substring(0, i);
  143.          } while(fm.stringWidth(t) < w - 10 && i++ < text.length());
  144.  
  145.          return text.substring(0, i - 1);
  146.       }
  147.    }
  148.  
  149.    public void drawCell(Graphics g, CellHints hints) {
  150.       Data data;
  151.       try {
  152.          data = this.getData();
  153.       } catch (DataNotAvailable var11) {
  154.          data = new ImageStringData(this.dataSource, "");
  155.       }
  156.  
  157.       Rectangle r = hints.bounds();
  158.       FontMetrics fm = g.getFontMetrics();
  159.       fm.getAscent();
  160.       String text = this.chopString(data.toString(), fm, r);
  161.       int sw = fm.stringWidth(text);
  162.       int imageOffset = 0;
  163.       g.getColor();
  164.       int origX = r.x;
  165.       this.drawButton(g, r);
  166.       Image im = data.toImage();
  167.       switch (hints.alignment()) {
  168.          case 0:
  169.             if (im != null) {
  170.                imageOffset = im.getWidth(this) + 5;
  171.             }
  172.  
  173.             if (imageOffset + sw + 2 + 5 <= r.width && im != null) {
  174.                r.x += 5;
  175.                r.y += 3;
  176.                g.drawImage(im, r.x, r.y, this);
  177.                r.y -= 3;
  178.             } else {
  179.                imageOffset = 5;
  180.             }
  181.  
  182.             r.x = origX + imageOffset + 2;
  183.             break;
  184.          case 1:
  185.             r.x = origX + (r.width - sw) / 2;
  186.             break;
  187.          case 2:
  188.             if (im != null) {
  189.                imageOffset = im.getWidth(this) + 5;
  190.             }
  191.  
  192.             if (sw + imageOffset + 2 + 5 <= r.width && im != null) {
  193.                r.x = r.x + r.width - sw - 5 - imageOffset - 2;
  194.                r.y += 3;
  195.                g.drawImage(im, r.x, r.y, this);
  196.                r.y -= 3;
  197.             }
  198.  
  199.             r.x = origX + r.width - sw - 5;
  200.       }
  201.  
  202.       g.setColor(hints.foreground());
  203.       g.drawString(text, r.x, r.y + fm.getAscent() + 2);
  204.    }
  205.  
  206.    void drawButton(Graphics g, Rectangle r) {
  207.       Color bg = Color.lightGray;
  208.       g.setColor(bg);
  209.       g.fillRect(r.x, r.y, r.width - 1, r.height);
  210.       g.setColor(Color.gray);
  211.       g.drawRect(r.x + 1, r.y + 1, r.width - 3, r.height - 3);
  212.       g.setColor(Color.black);
  213.       g.drawRect(r.x, r.y, r.width - 1, r.height - 1);
  214.       if (this.drawUp) {
  215.          g.setColor(Color.white);
  216.          g.drawLine(r.x + 1, r.y + 1, r.x + r.width - 2, r.y + 1);
  217.          g.drawLine(r.x + 1, r.y + 1, r.x + 1, r.y + r.height - 2);
  218.       } else {
  219.          g.setColor(Color.white);
  220.          g.drawLine(r.x + 1, r.y + r.height - 2, r.x + r.width - 2, r.y + r.height - 2);
  221.          g.drawLine(r.x + r.width - 2, r.y + 1, r.x + r.width - 2, r.y + r.height - 3);
  222.       }
  223.    }
  224.  
  225.    public boolean isCellTypeEditable() {
  226.       return false;
  227.    }
  228.  
  229.    public boolean actionEvent(Event e) {
  230.       return true;
  231.    }
  232.  
  233.    public boolean keyEvent(Event e) {
  234.       return true;
  235.    }
  236.  
  237.    public boolean canLoseFocus() {
  238.       return true;
  239.    }
  240.  
  241.    public boolean focusEvent(Event e) {
  242.       if (e.id == 1004) {
  243.          this.view.setCapture(this);
  244.          this.view.generateEvent(e, 55, this);
  245.       } else {
  246.          this.view.lostCapture();
  247.          this.view.generateEvent(e, 56, this);
  248.       }
  249.  
  250.       return true;
  251.    }
  252.  
  253.    public boolean loseFocusOnArrow() {
  254.       return false;
  255.    }
  256.  
  257.    public void activateCursor() {
  258.    }
  259.  
  260.    public void deactivateCursor() {
  261.    }
  262.  
  263.    public String stats() {
  264.       return "";
  265.    }
  266.  
  267.    public boolean imageUpdate(Image img, int flags, int x, int y, int w, int h) {
  268.       if ((flags & 192) != 0) {
  269.          return false;
  270.       } else if ((flags & 32) != 0) {
  271.          this.view.redraw(true);
  272.          return false;
  273.       } else {
  274.          return true;
  275.       }
  276.    }
  277. }
  278.